import json
from kafka import KafkaProducer, KafkaConsumer
import time

# 数据-信号灯丢包、时延
# mock 信号灯数据-lamp-qps（丢包）
LAMP_DATA = [
    {"dataFrom": "RSCU_MSG_ASN", "msgType": "RSI", "roadId": "179", "rscuSn": "BDOF2112032G06", "type": "delay",
     "value": ["1651904040.399", "0"]},
    {"dataFrom": "RSCU_MSG_ASN", "msgType": "MAP", "roadId": "322", "rscuSn": "BDOF2110202G05", "type": "delay",
     "value": ["1651904040.399", "0.3333333333333333"]},
    {"dataFrom": "RSCU_MSG_ASN", "msgType": "SPAT", "roadId": "322", "rscuSn": "BDOF2110202G05", "type": "delay",
     "value": ["1651904040.399", "0.3333333333333333"]},
    {"dataFrom": "RSCU_MSG_ASN", "msgType": "RSI", "roadId": "179", "rscuSn": "BDOF2112032G06", "type": "qps",
     "value": ["1651904040.399", "0"]},
    {"dataFrom": "RSCU_MSG_ASN", "msgType": "MAP", "roadId": "322", "rscuSn": "BDOF2110202G05", "type": "qps",
     "value": ["1651904040.399", "0.3333333333333333"]},
    {"dataFrom": "RSCU_MSG_ASN", "msgType": "SPAT", "roadId": "322", "rscuSn": "BDOF2110202G05", "type": "qps",
     "value": ["1651904040.399", "0.3333333333333333"]}

]

# 数据-rscu在线状态
RSCU_OLIINE_STATUS = [
    {
        "sn": "BDOF2112062G09",
        "name": "rscu_online_status",
        "device": "RSCU-296-146",
        "instance": "ote-bos1",
        "ipaddr": "172.29.1.1",
        "job": "node_exporter",
        "place": "亦庄",
        "rscuIp": "172.21.154.146",
        "rscuName": "RSCU296001",
        "value": [
            "1651904040.581",
            "0"
        ]
    },
    {
        "sn": "BDOF2112062G09",
        "name": "rscu_online_status",
        "device": "RSCU-296-146",
        "instance": "ote-bos1",
        "ipaddr": "172.29.1.1",
        "job": "node_exporter",
        "place": "亦庄",
        "rscuIp": "172.21.154.146",
        "rscuName": "RSCU296001",
        "value": [
            "1651904040.581",
            "0"
        ]
    }
]

# topic-信号灯丢包、时延
lamp_topic = 'v2x-quality-prometheus-lamp'
# topic-rscu 在线状态
rscu_topic = 'v2x-quality-prometheus-rscu-online-status'

if __name__ == '__main__':

    # consumer = KafkaConsumer('v2x-quality-prometheus-rscu-online-status',
    #                          bootstrap_servers=['10.138.56.210:8092','10.138.56.210:8093','10.138.56.210:8094'],
    #                          auto_offset_reset='earliest'
    #               )
    producer = KafkaProducer(bootstrap_servers=['10.252.104.131:9092'])

    # rscu 数据发送
    while True:
        # 30s推一次数据
        time.sleep(30)

        for msg in RSCU_OLIINE_STATUS:
            time_res = format(time.time(), '.3f')
            msg['value'][0] = time_res
            print("rscu_msg=%s" % msg)
            data = json.dumps(msg).encode()
            # 数据发送
            producer.send(rscu_topic, value=data)
            print("rscu_msg send success")

        # 信号灯数据发送
        for msg in LAMP_DATA:
            time_res = format(time.time(), '.3f')
            msg['value'][0] = time_res
            print("lamp_msg=%s" % msg)
            data = json.dumps(msg).encode()
            # 数据发送
            producer.send(lamp_topic, value=data)
            print("lamp_msg send success")